package com.magic.smartcity.view.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.navigation.Navigation;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.magic.smartcity.R;
import com.magic.smartcity.beans.ServiceListBean;
import com.magic.smartcity.utils.Constant;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @description:
 * @create: Create by Magic on 2022/2/2.
 */
public class HomeServiceAdapter extends BaseAdapter {
    private static final String TAG = HomeServiceAdapter.class.getSimpleName();

    private int mType;
    private List<ServiceListBean.RowsBean> mData = new ArrayList<>();
    private LinearLayout rootline = null;

    /**
     * @param type 传入1代表首页(只有9个服务应用)，2代表全部服务应用(All的服务应用)
     */
    public HomeServiceAdapter(int type) {
        mType = type;
    }

    public void setData(List<ServiceListBean.RowsBean> data) {
        mData.clear();
        mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mData.size() > 0) {
            if (mType == 1) {
                return 10;
            } else if (mType == 2) {
                return 1;
            }
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return mData.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_service_topic, parent, false);
        ImageView image = view.findViewById(R.id.service_icon);
        TextView text = view.findViewById(R.id.service_title);
        /**
         * 在这里可以定义点击应用服务后的跳转操作
         */
        switch (mData.get(position).getServiceName()) {
            case "停哪儿":
                image.setOnClickListener(v -> Navigation.findNavController(v).navigate(R.id.action_nav_home_to_parkHomeFragment));
                break;
            case "城市地铁":
                image.setOnClickListener(v -> Navigation.findNavController(v).navigate(R.id.action_nav_home_to_metroHomeFragment));
                break;
            case "智慧巴士":
                image.setOnClickListener(v -> Navigation.findNavController(v).navigate(R.id.action_nav_home_to_busHomeFragment));
                break;
            case "门诊预约":
                image.setOnClickListener(v -> Navigation.findNavController(v).navigate(R.id.action_nav_home_to_hospitalFragment));
                break;
            case "智慧交管":
                image.setOnClickListener(v -> {
                    Toast.makeText(parent.getContext(), mData.get(position).getServiceName() + "", Toast.LENGTH_SHORT).show();
                });
                break;
            case "生活缴费":
                image.setOnClickListener(v -> {
                    Toast.makeText(parent.getContext(), mData.get(position).getServiceName() + "", Toast.LENGTH_SHORT).show();
                });
                break;
            case "外卖订餐":
                image.setOnClickListener(v -> {
                    Toast.makeText(parent.getContext(), mData.get(position).getServiceName() + "", Toast.LENGTH_SHORT).show();
                });
                break;
            case "找房子":
                image.setOnClickListener(v -> {
                    Toast.makeText(parent.getContext(), mData.get(position).getServiceName() + "", Toast.LENGTH_SHORT).show();
                });
                break;
            case "看电影":
                image.setOnClickListener(v -> {
                    Toast.makeText(parent.getContext(), mData.get(position).getServiceName() + "", Toast.LENGTH_SHORT).show();
                });
                break;
            case "找工作":
                break;
            case "活动管理":
                break;
            case "数据分析":
                break;
        }
        switch (mType) {
            case 1://用于首页的应用服务渲染
                if (mType == 1 && position > 8) {
                    Glide.with(view).load(R.mipmap.assets).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(image);
                    text.setText("更多服务");
                    image.setOnClickListener(v -> {
                        Navigation.findNavController(v).navigate(R.id.action_nav_home_to_nav_more);
                    });
                } else {
                    Glide.with(view).load(Constant.Service_IP + mData.get(position).getImgUrl()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(image);
                    text.setText(mData.get(position).getServiceName());
                }
                break;
            case 2://用于更多服务的 自定义应用服务渲染

                break;
        }


        return mType == 1 ? view : rootline;
    }


}
